package com.mock.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import com.mock.dao.BusinessTypeDao;
import com.mock.model.BusinessType;
import com.mock.model.DataTable;
import com.mock.utils.HibernateUtil;

public class BusinessTypeDaoImpl extends GenericDaoImpl<BusinessType, Long>
		implements BusinessTypeDao {

	@Override
	public List<BusinessType> getAlBusinessType() {
		return super.getAll(BusinessType.class);
	}

	@Override
	public BusinessType getById(long id) {
		return super.getById(BusinessType.class, id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BusinessType> getBusinessByQuerry(DataTable dt) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.getTransaction().begin();
		Criteria criteria = session.createCriteria(BusinessType.class,"bt");
		
		// page
		criteria.setFirstResult(dt.iDisplayStart);
		criteria.setMaxResults(dt.iDisplayLength);
		
		// result
		if (dt.sSearch_1 .trim().length() != 0 ) {
			Criterion cr1 = Restrictions.ilike("bt.businessName","%"+dt.sSearch_1+"%");
			criteria.add(cr1);
		}
		
		if (dt.sSearch_2 .trim().length() != 0 ) {
			Criterion cr2 = Restrictions.ilike("bt.sICCode","%"+dt.sSearch_2+"%");
			criteria.add(cr2);
		}
		
		//
		
		List<BusinessType> lst = new ArrayList<BusinessType>(); 
		lst = criteria.list();
		session.getTransaction().commit();
		return lst;
		
	}

	@Override
	public int getRowCount() {
		return super.getRowCount(BusinessType.class);
	}

}
